package game.sprite;

import engine.j2d.Image;
import engine.j2d.Rectangle;

public class FSprite implements Sprite
{
	private Image image;
	private Image images[];
	private AnimationSprite anim;
	private float x = 0 ;
	private float y = 0 ;
	private float xs = 0 ;
	private float ys = 0 ;
	private Rectangle me = new Rectangle();
	private Rectangle him = new Rectangle();
	private boolean SpriteArte = false;

	public FSprite(String ref)
	{
		this(ref, 0, 0);
	}

	public FSprite(String ref, float x, float y)
	{
		image = new Image(ref);
		this.x = x;
		this.y = y;
	}

	public void setFrame(int counImagesOnX, int countImagesOnY, int nspeed, boolean loop)
	{
		images = new Image[counImagesOnX * countImagesOnY];
		int xi = image.getWidth() / counImagesOnX;
		int yi = image.getHeight() / countImagesOnY;
		int z = -1;
		for (int i = 0; i < countImagesOnY; i++) {
			for (int j = 0; j < counImagesOnX; j++)
			{
				z++;
				if (z < counImagesOnX * countImagesOnY)
				{
					images[z] = image.getSubImage(j * xi, i * yi, xi, yi);
				}
			}
		}
		anim = new AnimationSprite(images, x, y, nspeed, loop);
		SpriteArte = true;
	}
	
	public boolean isFinishAnimation() {
		return anim.isFinishAnimation();
	}

	public void setPoint(float x, float y)
	{
		anim.setX(x);
		anim.setY(y);
	}

	public void setX(float x)
	{
		anim.setX(x);
	}

	public void setY(float y)
	{
		anim.setY(y);
	}

	public float getHeight()
	{
		return images[0].getHeight();
	}

	public float getWidth()
	{
		return images[0].getWidth();
	}

	public float getX()
	{
		return anim.getX();
	}

	public float getY()
	{
		return anim.getY();
	}

	public void moveX(float speed)
	{
		xs += speed;
		anim.setX(xs);
	}

	public void moveY(float speed)
	{
		ys += speed;
		anim.setY(ys);
	}

	public void render(float x_pos, float y_pos)
	{
		anim.render(x+x_pos, y+y_pos);
	}

	public void render()
	{
		anim.render();
	}

	public boolean collide(Sprite oder)
	{
		me.setBounds(x, y, getWidth(), getHeight());
		him.setBounds(oder.getX(), oder.getY(), oder.getWidth(), oder
				.getHeight());
		return me.intersects(him);
	}

	public boolean getSpriteArte()
	{
		return SpriteArte;
	}
}
